import 'package:health_manager/common/routers/hm_router/adapter.dart';

import 'hm_navigator.dart';

/// Url匹配结果
class CHURLMatchResult {
  /// 路径
  final String pattern;

  /// 参数
  final Map<String, String> values;
  CHURLMatchResult(this.pattern, this.values);
}

/// Url匹配
class HMURLMatcher {
  /// method_name match
  /// description url结果匹配
  ///
  ///@param: url 完整链接
  ///@param: candidates  路径集合
  ///
  ///@return: CHURLMatchResult?
  CHURLMatchResult? match(String url, List<URLPattern> candidates) {
    final urlValue = url.urlValue;
    if (urlValue == null) return null;
    final scheme = urlValue.scheme;
    final path = urlValue.path;
    final pattern = candidates.firstWhere(
      (element) =>
          element.urlValue?.scheme == scheme && element.urlValue?.path == path,
      orElse: () => "",
    );
    return pattern == ""
        ? null
        : CHURLMatchResult(pattern, urlValue.queryParameters);
  }
}
